package com.dd.admin.business.sellJson.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.dd.admin.common.security.SecurityUtil;
import com.dd.admin.common.utils.StringUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.dd.admin.common.model.UpdateGroup;
import com.dd.admin.common.model.result.ResultBean;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotBlank;
import com.dd.admin.business.sellJson.entity.Selljson;
import com.dd.admin.business.sellJson.domain.SelljsonVo;
import com.dd.admin.business.sellJson.domain.SelljsonDto;
import com.dd.admin.business.sellJson.service.SelljsonService;

import java.util.List;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 订单json 前端控制器
 * </p>
 *
 * @author 727869402@qq.com
 * @since 2024-06-19
 */
@Api(tags = "订单json")
@RestController
public class SelljsonController {

    @Autowired
    SelljsonService sellJsonService;

    @ApiOperation(value = "订单json-分页列表")
    @ApiOperationSupport(order = 1)
    @GetMapping("/admin/sellJson/page")
    public ResultBean<IPage<SelljsonVo>> page(SelljsonDto sellJsonDto) {
        sellJsonDto.setShopId(SecurityUtil.getLoginUser().getDeptId());
        IPage<SelljsonVo> pageInfo = sellJsonService.selectSelljsonPage(sellJsonDto);
        return ResultBean.success(pageInfo);
    }

    @ApiOperation(value = "订单json-列表")
    @ApiOperationSupport(order = 2)
    @GetMapping("/admin/sellJson/list")
    public ResultBean<List<SelljsonVo>> list(SelljsonDto sellJsonDto) {
        sellJsonDto.setShopId(SecurityUtil.getLoginUser().getDeptId());
        List<SelljsonVo> list = sellJsonService.selectSelljsonList(sellJsonDto);
        return ResultBean.success(list);
    }

    @ApiOperation(value = "订单json-添加")
    @ApiOperationSupport(order = 3)
    @PostMapping("/admin/sellJson/add")
    public ResultBean<Selljson> add(@RequestBody @Validated SelljsonDto sellJsonDto) {
        Selljson sellJson = BeanUtil.copyProperties(sellJsonDto, Selljson.class);
        if(StringUtil.isNotEmpty(sellJson.getJsonId())){
            Boolean b = sellJsonService.updateById(sellJson);
            if(!b){
                //证明该订单已经删除 或者计算 重新添加挂单即可
                sellJsonService.save(sellJson);
            }
        }else{
            sellJsonService.save(sellJson);
        }
        return ResultBean.success(sellJson);
    }

    @ApiOperation(value = "订单json-查询")
    @ApiOperationSupport(order = 4)
    @GetMapping("/admin/sellJson/{sellJsonId}")
    public ResultBean<SelljsonVo> get(@PathVariable @NotBlank String sellJsonId) {
        Selljson sellJson = sellJsonService.getById(sellJsonId);
        SelljsonVo sellJsonVo = BeanUtil.copyProperties(sellJson,SelljsonVo.class);
        return ResultBean.success(sellJsonVo);
    }

    @ApiOperation(value = "订单json-修改")
    @ApiOperationSupport(order = 5)
    @PostMapping("/admin/sellJson/update")
    public ResultBean<Selljson> update(@RequestBody @Validated(UpdateGroup.class) SelljsonDto sellJsonDto) {
        Selljson sellJson = BeanUtil.copyProperties(sellJsonDto, Selljson.class);
        sellJsonService.updateById(sellJson);
        return ResultBean.success(sellJson);
    }

    @ApiOperation(value = "订单json-删除")
    @ApiOperationSupport(order = 6)
    @GetMapping("/admin/sellJson/delete/{sellJsonId}")
    public ResultBean<Selljson> delete(@PathVariable @NotBlank String sellJsonId) {
        Boolean b = sellJsonService.removeById(sellJsonId);
        return ResultBean.success(b);
    }
}
